package teacher.flower;

public class FlowerServiceImpl implements FlowerService{
    @Override
    public void update() {
        System.out.println("请输入销售编号：");
        String uid = FlowerService.INPUT.next();
        // 拿到uid去数组中遍历查找有没有这个id的对象 , 没有就提示
        // 假设没有
        boolean flag = false;
        // 遍历数组
        for (FlowerOrder order : FlowerService.orderList) {
            if(order.getId().equals(uid)){
                flag = true;
                break;
            }
        }
        // 判断flag
        if(flag == false){ // 没有uid的订单
            System.out.println("对不起,您要修改的订单号不存在！");
            return;
        }
        // 有uid的订单
        System.out.println("您要修改的订单详细信息如下：");
        System.out.println();
        // 输出表头信息
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        for (FlowerOrder order : FlowerService.orderList) {
            if (order.getId().equals(uid)){
                System.out.println(""+order.getId()+"\t"+order.getName()+"\t"+order.getNumber()+"\t"+order.getPrice()+"\t"+order.getDate()+"\t"+order.getSalEmp()+"\t"+order.getBeiZhu()+"");
            }
        }
        System.out.println();
        // 录入新的修改后的信息
        System.out.println("请输入修改后的鲜花名称：");
        String newName = FlowerService.INPUT.next();
        System.out.println("请输入修改后的销售数量：");
        int newNumber = FlowerService.INPUT.nextInt();
        System.out.println("请输入修改后的鲜花价格：");
        double newPrice = FlowerService.INPUT.nextDouble();
        System.out.println("请输入修改后的销售日期:(yyyy-MM-dd)");
        String newDate = FlowerService.INPUT.next();
        System.out.println("请输入修改后的销售员：");
        String newSalEmp = FlowerService.INPUT.next();
        System.out.println("请输入修改后的备注：");
        String newDecript = FlowerService.INPUT.next();
        // 封装对象
        FlowerOrder order = new FlowerOrder(uid, newName, newNumber, newPrice, newDate, newSalEmp, newDecript);
        System.out.println("您确定压迫修改吗？(y/n)");
        String conf = FlowerService.INPUT.next();
        if("y".equals(conf)){
            // 将 order 替换数组中id为uid的对象元素
            for (int i = 0; i < FlowerService.orderList.length; i++) {
                FlowerOrder fo = FlowerService.orderList[i];
                if(fo.getId().equals(order.getId())){
                    FlowerService.orderList[i] = order;
                    break;
                }
            }
            System.out.println("修改成功！");
        }

        // 重新查询一遍
        findAll();
    }
    @Override
    public void findAll() {
        // 1. 打印表头
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        // 2. 打印查询到的5条数据
        for (FlowerOrder flowerOrder : FlowerService.orderList) {
            System.out.println(""+flowerOrder.getId()+"\t"+flowerOrder.getName()+"\t"+flowerOrder.getNumber()+"\t"+flowerOrder.getPrice()+"\t"+flowerOrder.getDate()+"\t"+flowerOrder.getSalEmp()+"\t"+flowerOrder.getBeiZhu()+"");
        }
        System.out.println();
        System.out.println();
    }
}
